package com.clqteam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.clqteam.model.Employee;
import com.clqteam.service.insertShService;
import com.clqteam.serviceimpl.insertShServiceImpl;

public class insertShController extends HttpServlet{
	/**
	 * HC
	 */
	
	
	insertShService  ins = new insertShServiceImpl();
	
	
	          @Override
	        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	        	  doPost(req, resp);
	        }
	          
	          
	          @Override
	        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	        	  
	        	//获取系统时间
             	 Date date=new Date();
             	 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             	 String drTime=format.format(date);
             
             	 //新增入库
                        String pName = req.getParameter("pName");
                        String pType=req.getParameter("pType");
                  		String pAmounts=req.getParameter("pAmount");
                  		int pAmount  = Integer.parseInt(pAmounts);
                  		String pClass=req.getParameter("pClass");
                  		String pCosts=req.getParameter("pCost");
                  		double pCost  = Double.parseDouble(pCosts);
                  		String pPrices=req.getParameter("pPrice");
                  		double pPrice  = Double.parseDouble(pPrices);
                  		String pTime=req.getParameter("pTime");
                  		String supIds=req.getParameter("supId");
                  		int supId  = Integer.parseInt(supIds);
                  		int m = ins.insertShByAllInfo(pName, pType, pAmount, pClass, pCost, pPrice, drTime, supId);
                  		String mm = Integer.toString(m);
                		PrintWriter pw = resp.getWriter();
                		pw.write(mm);
                		
                		
                		 //写入日志
                      	 HttpSession session = req.getSession();
                      	 Employee emp = (Employee) session.getAttribute("employee");
                      	 String eName = emp.geteName();
                      	 int eId = emp.geteId();
                      	 int ssss =0;
                      	 StringBuffer drInfos = new StringBuffer();
                     		drInfos.append("用户");
                     		drInfos.append(eName);
                     		drInfos.append("新增商品入库,商品编号为");
                     		drInfos.append(ssss);
                     		String drInfo = drInfos.toString();
                            ins.setdirInfo(drInfo, drTime, eId, eName);
                        //写入订单及收支
                       	 String code=drTime+eId;
                       	 int vId=0;
                       	 int oClassId = 4;
                       	 double oMoney = pAmount*pCost;
                            ins.setOrder(drTime, supId, oClassId, oMoney, eId, vId, code);
                            int oId = ins.getOidByCode(code);
                            int pid = ins.getPidByAllInfo(pName, pType, pAmount, pClass, pCost, pPrice, drTime, supId);
                            ins.setOrderInfo(oId, pid, pAmount, pCost);
                            ins.setAccountant(oId);
	        	  
	        }
}
